package com.zkc.bootnew.webserviceimpl;


import com.alibaba.fastjson.JSON;
import com.zkc.bootnew.controller.utils.DigestUtil;
import com.zkc.bootnew.controller.utils.RedisUtils;
import com.zkc.bootnew.controller.utils.UserAgentUtil;
import com.zkc.bootnew.webservice.RpcTokenService;
import com.zkc.springbootnew.bean.Contact_user;
import com.zkc.springbootnew.utils.ErrorCode;
import com.zkc.springbootnew.utils.GotripException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.zkc.bootnew.controller.utils.Constants.RedisExpire.SESSION_TIMEOUT;


@Component
@Service
public class RpcTokenServiceImpl implements RpcTokenService {

    @Resource
    private RedisUtils redisUtils;




    /**
     * 生成token
     *
     * @param user 用户信息
     * @param userAgent  浏览器内核版本
     * @return
     */
    @Override
    public String generateToken(Contact_user user, String userAgent) {
          //[MOBILE|PC]-userCode(md5)-userId-yyyyMMddHHmmss-浏览器的标识
          StringBuffer sbToken=new StringBuffer("token:");
          if(UserAgentUtil.checkAgent(userAgent)){
             //移动
             sbToken.append("MOBILE-");
          }else{
              //pc
              sbToken.append("PC-");
          }
          String md5UserCode= DigestUtil.hmacSign(user.getContactalias());
          sbToken.append(md5UserCode).append("-");
          sbToken.append(user.getId()).append("-");
          //yyyyMMddHHmmss-
          String createTime=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
          sbToken.append(createTime).append("-");
          String md5UserAgent=DigestUtil.hmacSign(userAgent,6);
          sbToken.append(md5UserAgent);

          return sbToken.toString();
    }




    /**
     * 保存token
     *
     * @param token      token令牌
     * @param user 用户信息
     */
    @Override
    public void saveToken(String token, Contact_user user) {
           if (token.contains("PC-")){
               redisUtils.set(token, SESSION_TIMEOUT, JSON.toJSONString(user));
           }else if(token.contains("MOBILE-")){
               redisUtils.set(token, JSON.toJSONString(user));
           }
    }

    /**
     * 验证token是否有效
     *
     * @param token     token令牌
     * @param userAgent 浏览器内核版本信息
     * @return 验证结果
     */
    @Override
    public boolean verifyToken(String token, String userAgent) {
        //token是否存在
        if(!redisUtils.exist(token)){
        return  false;
        }
        //创建token浏览器和当前的浏览器是否一致
        String md5UserAgent=DigestUtil.hmacSign(userAgent,6);
        return token.contains(md5UserAgent);
    }



    /**
     * 删除Token
     * @param token
     */
    @Override
    public void removeToke(String token) {
        redisUtils.expire(token,3);
    }

    /**
     * 根据token获取用户
     *
     * @param token     token令牌
     * @param userAgent 浏览器标识
     * @return
     */
    @Override
    public Contact_user  getGotripUser(String token, String userAgent) throws GotripException {

        //验证浏览器
        if(!this.verifyToken(token,userAgent)){
            throw new GotripException("Token无效", ErrorCode.AUTH_TOKEN_INVALID);
        }
        //获取用户信息
        String contactUserJson=(String)redisUtils.get(token);
        return JSON.parseObject(contactUserJson,Contact_user.class);
    }


















}
